package physics;
import java.awt.Color;
import java.awt.Graphics2D;

/**
 * @author Robert C. Duvall edited by Sam Rang and Kennan Raju
 */
public class Spring extends Connector {
    private double myLength;
    private double myK;

    /**
     * Spring inherits the basic methods of the Connector
     * abstract class. Follows simple Hooke's law with no
     * dampening or driving.
     * @param start Mass at one end of the Spring
     * @param end Mass at the other end of the Spring
     * @param length The rest length of the spring
     * @param kVal The k Constant of the Spring
     */
    public Spring (Mass start, Mass end, double length, double kVal) {
        super(start, end, length, kVal);
        myLength = length;
        myK = kVal;
    }

    /**
     * Changes the color of the color of the spring based on the current
     * length and the rest length.
     * @param len Current Length
     * @param restLength Rest length of the spring
     * @param pen Graphics2D being written to
     */
    @Override
    public void checkStyle (double len, double restLength, Graphics2D pen) {
        if (len < myLength) {
            pen.setColor(Color.BLUE);
        }
        else {
            pen.setColor(Color.RED);
        }
    }

    @Override
    public Force setForce (double dx, double dy) {
        Force f = new Force(Force.angleBetween(dx, dy), myK
                * (myLength - Force.distanceBetween(dx, dy)));
        return f;
    }
}
